package com.leps.nq.sys.controller;

import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.sys.entity.SysRole;
import com.leps.nq.sys.entity.SysUser;
import com.leps.nq.sys.service.ISysRoleService;
import com.leps.nq.sys.service.ISysUserService;
import com.leps.nq.sys.vo.CreateUserVo;
import com.leps.nq.sys.vo.GrantRoles2Vo;
import com.leps.nq.sys.vo.UserInfoVo;
import com.leps.nq.sys.vo.UserQueryModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-20
 */
@RestController
@RequestMapping("/sys/sys-user")
@Api(tags = "用户管理")
public class SysUserController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    /**
     * 获取用户列表
     */
    @GetMapping
    @ApiOperation("用户列表")
    public R<PageVo<SysUser>> list(UserQueryModel userQueryModel) {
        PageVo<SysUser> userPageVo = userService.selectUserListPage(userQueryModel);
        return RBuilder.ok(userPageVo);
    }

    @ApiOperation("获取指定用户的基本信息")
    @GetMapping("/{userId}")
    public R<SysUser> profileOf(@PathVariable Long userId) {
        SysUser sysUser = userService.getById(userId);
        return RBuilder.ok(sysUser);
    }

    @GetMapping(value = {"/details/{userId}"})
    @ApiOperation("获取指定用户的详细信息(包含拥有的角色、菜单、权限码；服务于哪些客户、区块、场站/平台；属于哪个任务小队)")
    public R<UserInfoVo> detailsOf(@PathVariable Long userId) {
        UserInfoVo userInfoVo = this.userService.selectDetailsById(userId);
        return RBuilder.ok(userInfoVo);
    }

    @PostMapping
    @ApiOperation("新增用户")
    @Log(title = "系统用户", businessType = BusinessType.INSERT)
    public R save(@RequestBody CreateUserVo createUserVo) {
        boolean b = this.userService.createUser(createUserVo);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("更新用户信息")
    @PutMapping
    @Log(title = "系统用户", businessType = BusinessType.UPDATE)
    public R update(@RequestBody SysUser user) {
        boolean b = this.userService.updateUser(user);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("删除用户")
    @DeleteMapping
    @Log(title = "系统用户", businessType = BusinessType.DELETE)
    public R remove(@RequestBody List<Long> userIds) {
        boolean b = this.userService.removeBatchByIds(userIds);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("管理员重置密码")
    @PutMapping("/resetPwd")
    public R resetPwd(Long userId) {
        boolean b = this.userService.resetPwd(userId);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("用户自身更新密码")
    @PutMapping("/changePwd")
    public R changePwd(String oldPwd, String newPwd) {
        boolean b = this.userService.changePwdBySelf(oldPwd, newPwd);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("修改用户状态：0启用 1禁用")
    @PutMapping("/changeStatus")
    @Log(title = "系统用户", businessType = BusinessType.UPDATE)
    public R changeStatus(Long userId, String status) {
        boolean success = this.userService.changeStatus(userId, status);
        return RBuilder.ofBoolean(success);
    }

    @ApiOperation("根据用户编号获取角色")
    @GetMapping("/roles")
    public R<List<SysRole>> authRole(Long userId) {
        List<SysRole> roles = this.userService.selectRolesByUserId(userId);
        return RBuilder.ok(roles);
    }

    @ApiOperation("分配角色给用户")
    @PutMapping("/grantRoles")
    @Log(title = "系统用户", businessType = BusinessType.OTHER)
    public R grantRoles(Long userId, Long[] roleIds) {
        boolean success = this.userService.grantRoles(userId, roleIds);
        return RBuilder.ofBoolean(success);
    }

    @ApiOperation("分配角色、客户、区块、平台给用户")
    @PostMapping("/grantRoles2")
    @Log(title = "系统用户", businessType = BusinessType.OTHER)
    public R grantRoles2(@RequestBody GrantRoles2Vo vo) {
        boolean b = this.userService.grantRoles2(vo);
        return RBuilder.ofBoolean(b);
    }
}
